(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取下拉列表
	function getlinklist(selectlist,selectid,type){
	   $.ajax({
			url:'http://web.icecn.net/match/getEnumInfo.json',
			type:'POST',
			async:true,
			data:{
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				dataset=data.t;
				var d=new Array();
				if(dataset && dataset.length > 0){
				   for(var i=0;i<dataset.length;i++){
					   if(dataset[i].enumType==type){
						   d.push(dataset[i])
					   }  
				   }
				   dataset=d;
				   var html = template(selectlist,{dataset:dataset});
				   $("#"+selectid).html(html);
				};
			}
	   });	
	}
	//获取列表
	function getlist(curpage,pagenum){
		var troopsName=$("#troopsName").val()
		var idCard=$("#idCard").val()
		var phone=$("#phone").val()
		var nickName=$("#nickName").val()
		var gameType=$("#gameType option:selected").attr("data-id")==0?"":$("#gameType option:selected").attr("data-id")
	   $.ajax({
			url:'http://web.icecn.net/troops/findTroopsList.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				troopsName:troopsName,
				idCard:idCard,
				phone:phone,
				nickName:nickName,
				gameType:gameType
				},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
			   if(data.code == 200 ){
				 if(data.t!=null&&data.t!=""){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")};
					}
				 }else{
					$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>");
				 };
			   }
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
				curpage=1
			  	getlist(curpage,pagenum)
			})
			
			//封禁解封战队
			$(".content-wrapper").on("click", '.delete', function() {
				$("#modal-delete .modal-body").attr("data-id",$(this).attr("data-id"))
				$("#modal-delete .modal-body").html("确认"+$(this).html()+"吗？")
			})
			//确认封禁解封战队
			$(".btndelete").click(function(){
			   var id=$("#modal-delete .modal-body").attr("data-id");
			   $.ajax({
					url:'http://web.icecn.net/troops/bannedTroops.json',
					type:'POST',
					async:true,
					data:{
						troopsId:id
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code == 200 ){
							$("#modal-delete").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("操作失败")	
						}
					}
			   });
		    })
			//队伍信息
			$(".content-wrapper").on("click", '.information', function() {
				var id=$(this).attr("data-id");
				$("#modal-information .modal-body").attr("data-id",id);
			    $.ajax({
					url:'http://web.icecn.net/troops/findTroopsInfo.json',
					type:'POST',
					async:true,
					data:{
						troopsId:id
					},
			         xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
					  if(data.code==200){
						 var troopsMember=data.t.troopsMember;
						 if(troopsMember.length>0){
						 $(".teamleader span").html("队长("+troopsMember[0].nickName+")-"+troopsMember[0].idCard+"-"+troopsMember[0].readName+"")
						   var a="";
						   for(var i=1;i<troopsMember.length;i++){
							   a+="<li>队员("+troopsMember[i].nickName+")<br/>"+troopsMember[i].serverType+"-"+troopsMember[i].gameAccount+"</li>"
						   }
						   $("#modal-information ul").html(a)
						 }else{
						   $("#modal-information ul").html("") 
						 }
						 var matchSchema=data.t.matchSchema;
						 if(matchSchema.length>0){
						   var b="";
						   for(var i=0;i<matchSchema.length;i++){
							   b+="<tr><td>"+matchSchema[i].matchName+"</td><td>"+matchSchema[i].ranking+"</td></tr>"
						   }
						   $("#modal-information .table tbody").html(b)
						 }else{
						   $("#modal-information .table tbody").html("<tr><td>无比赛记录</td></tr>") 
						 } 
					  }
					}
				 });   
			})
			//更换队长
			$(".content-wrapper").on("click", '.change', function() {
				var id=$("#modal-information .modal-body").attr("data-id")
				$.ajax({
					url:'http://web.icecn.net/troops/findTroopsInfo.json',
					type:'POST',
					async:true,
					data:{
						troopsId:id
					},
					dataType: "json",
					xhrFields:{withCredentials:true},
					success: function(data){
					  if(data.code==200){
						 var a="";
						 var troopsMember=data.t.troopsMember;
						 if(troopsMember.length>0){
						   for(var i=1;i<troopsMember.length;i++){
							   a+="<li><input type='radio' data-id='"+troopsMember[i].userId+"' name='Member'>队员("+troopsMember[i].nickName+")</li>"
						   }
						   $("#modal-change ul").html(a)
						 }else{
						   $("#modal-change ul").html("") 
						 }
					  }
					}
			   });
			})
			//确认更换队长
			$(".btnchange").click(function(){
				var id=$("#modal-information .modal-body").attr("data-id");
				var idCard=$(".idcard").val();
				var readName=$(".readName").val();
				var userId=$("#modal-change .modal-body [name='Member']:checked ").attr("data-id");
				$.ajax({
					url:'http://web.icecn.net/troops/changeTroopsCaptain.json',
					type:'POST',
					async:true,
					data:{
						troopsId:id,
						userId:userId,
						idCard:idCard,
						readName:readName
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
					  if(data.code==200){
						 $("#modal-change").modal('hide');
						 getlist(curpage,pagenum)
						 $.ajax({
							url:'http://web.icecn.net/troops/findTroopsInfo.json',
							type:'POST',
							async:true,
							data:{
								troopsId:id
							},
							xhrFields:{withCredentials:true},
							dataType: "json",
							success: function(datas){
							  if(datas.code==200){
								 var troopsMember=datas.t.troopsMember;
								 if(troopsMember.length>0){
								 $(".teamleader span").html("队长("+troopsMember[0].nickName+")-"+troopsMember[0].idCard+"-"+troopsMember[0].readName+"")
								   var a="";
								   for(var i=1;i<troopsMember.length;i++){
									   a+="<li>队员("+troopsMember[i].nickName+")<br/>"+troopsMember[i].serverType+"-"+troopsMember[i].gameAccount+"</li>"
								   }
								   $("#modal-information ul").html(a)
								 }else{
								   $("#modal-information ul").html("") 
								 }
							  }
							}
						 });   
					  }else{
						 alert(data.msg)
					  }
					}
			   });
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
			getlinklist("selectlist","gameType",2);
		}
	};
	Page.init();
})();